package org.black.phoneUtility.sms.reply.content.broadcastReceiver;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.app.activity.RingtoneChangeActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.PreferenceManager;
import android.util.Log;

public class RingtoneChangeBroadcastReceiver extends BroadcastReceiver {
	private static final String LOG_TAG = RingtoneChangeBroadcastReceiver.class
			.getName();

	@Override
	public void onReceive(final Context context, Intent intent) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(1000l);
				} catch (InterruptedException e) {
					Log.e(LOG_TAG, "Sleep 1000 milliseconds Fail!", e);
				}
				SharedPreferences preferences = PreferenceManager
						.getDefaultSharedPreferences(context);
				String originalRingtoneKey = context
						.getString(R.string.preference_sms_reply_original_ringer_mode);
				AudioManager audioManager = (AudioManager) context
						.getSystemService(Context.AUDIO_SERVICE);
				if (preferences.contains(originalRingtoneKey) == true
						&& audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
					Intent dialogIntent = new Intent(context,
							RingtoneChangeActivity.class);
					dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(dialogIntent);
				}

			}
		}).start();

	}

}
